﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace xiangmu1
{
    public partial class HY_GL : Form
    {
        public HY_GL()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            HuiYuanKaLeiXing();
            //ChaXunHuiYuan();
            string sql = @" select VpId, VpName, VpKaHao, VpPhone, VpSex, TypeName,ZheKou, 
                VpMoney, VpBanKaTime, VpShengRi from VipInfo vi inner join VipType vt on vi.VpKaType=vt.VipId";
            DataTable dt = FrmHotel.DBHelper.SelectForTable(sql);
            dataGridView1.DataSource = dt;
        }
        public void ChaXunHuiYuan()
        {
         
            int type = cbo_xialakaleixing.SelectedIndex;
            string sql = @" select  VpId,VpName, VpKaHao, VpPhone, VpSex, TypeName,ZheKou, 
                VpMoney, VpBanKaTime, VpShengRi from VipInfo vi inner join VipType vt on vi.VpKaType=vt.VipId where 1=1";
            int leixing = Convert.ToInt32(cbo_xialakaleixing.SelectedValue);
            if (leixing != 0)
            {
                sql += string.Format(" and VpKaType=" + leixing);
            }
            string khao = txt_huiyuanchaxun.Text;
            if (!string.IsNullOrEmpty(khao))
            {
                sql += string.Format(" and VpKaHao like '%{0}%'",khao);
            }

            string phoer = textBox1.Text;
            if (!string.IsNullOrEmpty(phoer))
            {
                sql += string.Format(" and VpPhone like '%{0}%'", phoer);
            }
            DataTable dt = FrmHotel.DBHelper.SelectForTable(sql);
            dataGridView1.DataSource = dt;

        }
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            H_Y_TJ a = new H_Y_TJ();
            DialogResult dr = a.ShowDialog();
            if (dr == DialogResult.OK)
            {
                ChaXunHuiYuan();
            }
        }
      
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            object o = this.dataGridView1.SelectedRows[0].Cells[0].Value;
            int id = Convert.ToInt32(o);

            H_Y_CZ a = new H_Y_CZ();
            a.id = id;
            a.kahao= dataGridView1.CurrentRow.Cells[2].Value.ToString();
            DialogResult dr = a.ShowDialog();

            if (dr == DialogResult.OK)
            {
                ChaXunHuiYuan();
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            H_Y_XG a = new H_Y_XG();
            a.kahao = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            DialogResult dr = a.ShowDialog();
            if (dr == DialogResult.OK)
            {
                ChaXunHuiYuan();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
             string sql = string.Format(@" select  VpId,VpName, VpKaHao, VpPhone, VpSex, TypeName,ZheKou, 
                VpMoney, VpBanKaTime, VpShengRi from VipInfo vi inner join VipType vt on vi.VpKaType=vt.VipId where VpStateId=0 and VpKaHao like '{0}' or VpPhone like '{1}'", txt_huiyuanchaxun.Text,txt_huiyuanchaxun.Text);
             DataTable dt = FrmHotel.DBHelper.SelectForTable(sql);
            dataGridView1.DataSource = dt;
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要删除吗？", "提示窗口", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
            if (dr == DialogResult.No)
            {
                return;
            }
            string kahao = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            string chaxun = string.Format("select VpMoney from VipInfo where VpId={0}",kahao);
            int shu = Convert.ToInt32(FrmHotel.DBHelper.SelectOne(chaxun));
            if (shu > 0)
            {
                MessageBox.Show("会员卡还有余额不可以删除！");
            }
            else
            {
                string sql = string.Format("delete  VipInfo  where VpId={0}", kahao);
                int hang =Convert.ToInt32( FrmHotel.DBHelper.ExecuteUpdate(sql));

                string sql1 = string.Format("delete  CongZInfo  where CzKaHao={0}", kahao);
                int hang1 = Convert.ToInt32(FrmHotel.DBHelper.ExecuteUpdate(sql1));
                if (hang > 0 && hang1 > 0)
                {
                    MessageBox.Show("删除成功！");
                    ChaXunHuiYuan();
                }
                else
                {
                    MessageBox.Show("删除成功！");
                    ChaXunHuiYuan();
                }
            }
        }
        
        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要删除吗", "提示窗口", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
            if (dr == DialogResult.No)
            {
                return;
            }
            string kahao = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            string sql = "delete VipInfo where VpKaHao=" + kahao;
            int hang = FrmHotel.DBHelper.ExecuteUpdate(sql);
            if (hang > 0)
            {
                MessageBox.Show("删除成功");
                ChaXunHuiYuan();
            }
            else
            {
                MessageBox.Show("删除失败");
            }
        }

        private void toolStripButton5_Click_1(object sender, EventArgs e)
        {
            H_Y_CZJL a = new H_Y_CZJL();
            DialogResult dr = a.ShowDialog();
        }
        public void HuiYuanKaLeiXing()
        {
            string sql = "select * from VipType";
            DataTable dt = FrmHotel.DBHelper.SelectForTable(sql);
            DataRow dr = dt.NewRow();
            dr["VipId"] = 0;
            dr["TypeName"] = "全部";
            dt.Rows.InsertAt(dr, 0);
            cbo_xialakaleixing.DisplayMember = "TypeName";
            cbo_xialakaleixing.ValueMember = "VipId";
            cbo_xialakaleixing.DataSource = dt;
        }

        private void cbo_xialakaleixing_SelectedIndexChanged(object sender, EventArgs e)
        {
            ChaXunHuiYuan();
//            if(cbo_xialakaleixing.SelectedIndex==0)
//            {
//                string sql1 = @" select  VpId,VpName, VpKaHao, VpPhone, VpSex, TypeName,ZheKou, 
//                VpMoney, VpBanKaTime, VpShengRi from VipInfo vi inner join VipType vt on vi.VpKaType=vt.VipId where VpStateId=0";
//                DataTable dt1 = FrmHotel.DBHelper.SelectForTable(sql1);
//                dataGridView1.DataSource = dt1;
//            }
//            else
//            {
//                string sql = string.Format(@" select  VpId,VpName, VpKaHao, VpPhone, VpSex, TypeName,ZheKou, 
//                VpMoney, VpBanKaTime, VpShengRi from VipInfo vi inner join VipType vt on vi.VpKaType=vt.VipId where VpStateId=0 and VpKaType={0}", cbo_xialakaleixing.SelectedIndex);
//                DataTable dt = FrmHotel.DBHelper.SelectForTable(sql);
//                dataGridView1.DataSource = dt;
//            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void txt_huiyuanchaxun_TextChanged(object sender, EventArgs e)
        {
            ChaXunHuiYuan();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            ChaXunHuiYuan();
        }
    }
}
